/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.listview;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.provider.PhonebookSharingManager;
import com.phonebooksharing.android.provider.PhonebookSharing.DashboardSync;
import com.phonebooksharing.android.widgets.pagination.PaginatorListView;
import com.phonebooksharing.dto.dashboard.DashboardReaderItem;
import com.phonebooksharing.dto.friend.ContactItem;

@SuppressWarnings("unchecked")
public abstract class ReadableListView extends PaginatorListView {
    
    public ReadableListView(Context dv, int qty, TextView counter) {
        super(dv, qty, counter);
    }
    
    private ArrayList<DashboardReaderItem<ContactItem>> checkList = new ArrayList<DashboardReaderItem<ContactItem>>();
    
    protected View.OnClickListener checkListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView iv = (ImageView)v.findViewById(R.id.check);
            DashboardReaderItem<ContactItem> vh = (DashboardReaderItem<ContactItem>)v.getTag();
            
            if(!vh.isCrossed()) {
                iv.setImageState(new int[] { android.R.attr.state_checked }, true);
                iv.setTag("1");
                checkList.add(vh);
                vh.setCrossed(true);
            } else {
                iv.setImageState(new int[] { }, true);
                iv.setTag("0");
                checkList.remove(vh);
                vh.setCrossed(false);
            }
            
            final int s = checkList.size();
            if(s == 1) {//display actions
                showActionPanel();
            } else if (s == 0) {//display actions
                hideActionPanel();
            }
        }
    };
    
    private View actionsPanel;
    private boolean shown;
    private void showActionPanel() {
        View v = actionsPanel;
        if (shown) return;
        if (v == null) {
//            v = ((View) getParent().getParent()).findViewById(R.id.dash_actions);
            v = ((ViewStub)((View)getParent().getParent()).findViewById(R.id.actionStub)).inflate();
        }
        v.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_in));
        v.setVisibility(View.VISIBLE);
        //setup button archive
        v.findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dashAction(DashboardSync.ACTION_ARCHIVE);
            }
        });
        
        //setup button archive
        v.findViewById(android.R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dashAction(DashboardSync.ACTION_DELETE);
            }
        });
        
        actionsPanel = v;
        shown = true;
    }
    
    static class ViewHolder {
        ImageView icon, icon2, type, check;
        TextView name, time, text1, text2;
        View container, border;
        long id;
    }
    
    protected void dashAction(final int action) {
        Toast.makeText(getContext(), "This action is going to be performed on the next Sync!", Toast.LENGTH_SHORT).show();
        
        ArrayList<DashboardReaderItem<ContactItem>> l = checkList;
        ArrayList<DashboardReaderItem<?>> list = adapter.getInternalList();
        Context c = getContext();
        for(DashboardReaderItem<ContactItem> id : l) {
            PhonebookSharingManager.addDashboardSync(c, id.getId(), action);
            list.remove(id);
        }
        checkList.clear();
        hideActionPanel();
        adapter.notifyDataSetChanged();
    }

    private void hideActionPanel() {
        actionsPanel.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_out));
        actionsPanel.setVisibility(View.GONE);

        shown = false;
    }
}
